<?php
namespace Fn;

//use Think\Model;
class Export extends Base {

	static function csv($data,$field='',$filename='',$top=true, $sep_char = ','){

		$filename = iconv("UTF-8", "GBK",$filename);
	
		$csv = new ParseCsv();
		$csv->output_filename = null;		if(empty($filename)){
			$filename=date("Y-m-d H.i.s").".csv";
		}
		$data = $csv->output (null, $data, $field);
        
		header('Content-Type: text/html; charset=utf-8');
		header("Content-type: application/csv");
		header('Content-Disposition: attachment; filename='.$filename);
		echo mb_convert_encoding($data,'GB2312', 'UTF-8');
		exit;
	}
	

	//		self::saveCsv($data,$field,'111222333.csv');

	static function saveCsv($data,$field='',$fileName='',$fieldToFirstRow=false,$append=false){
		static $csv;
		if(is_null($csv)){
			$csv = new ParseCsv();
		}
		// 给标题转码   
		if(is_array($field)){
		    foreach ($field as $k=>$v){
		        $field[$k] = iconv('utf-8', 'gbk', $v);
		    }
		}
		if($fieldToFirstRow){
			array_unshift($data,$field);
		}

		$r = $csv->save($fileName, $data, $append, $field);
		return $r? $filename: false;
	}

	
	static function xlsBody($data,$field='',$top=true)
	{	
		if(empty($field)){
			reset($data);
			$field = array_keys($data[key($data)]);
			$field = array_combine($field,$field);
		}
		$r = '<style>
				.xtable td{padding:5px 10px 5px 10px; text-align:left;}
				.xrow1 td{background:#bce4d7;}
				.xrow2 td{background:#ddf1ea;}
				.xtitle td{background:#59bb9b; color:#FFF; font-size:14px; font-weight:bold; text-align:center;}
				.xtable {border: solid 1px #59bb9b;}
				</style>
			<table cellpadding="0" cellspacing="1" class="xtable">';
		if($top){
			$r.= '<tr class="xtitle">';
			foreach ($field as $k=>$v){
				$r.= isset($v[$k])? '<td>'.htmlspecialchars($v).'</td>': '<td>&nbsp;</td>';				
			}
			$r.= '</tr>';
		}
		$i=0;
		foreach ($data as $v){
			$cls = ($i%2)===0? 'xrow1': 'xrow2';
			$r.= '<tr class="'.$cls.'">';
			foreach ($field as $k=>$fieldV){
				$r.= isset($v[$k])? '<td>'.htmlspecialchars($v[$k]).'</td>' : '<td>&nbsp;</td>';
			}
			$r.= '</tr>';
			$i++;
		}
		$r.= '</table>';
		return $r;
	}

	static function saveXls($data, $field='' ,$filename='')
	{
		$content = self::xlsBody($data, $field);
		 $fileHead= ''.
				'<html xmlns="http://www.w3.org/1999/xhtml">'.
					'<head>'.
						'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.
						'<title>'.basename($filename).'</title>'.
					'</head><body>';
		file_put_contents( $filename, $fileHead.$content.'</body></html>');
		return $filename;
	}
	
	static function xls($data,$field=array(),$fileName='',$cfg=array(),$top=true){
		if(!$fileName){
			$filename=date("Y-m-d H.i.s");
		}
		$fileName = iconv("UTF-8", "GBK",$fileName);
		header("Content-type: application/xls");
		header('Content-Disposition: attachment; filename='.$fileName);
		$cfg = array_merge(array(
			'headAppend'=>'','style'=>'',
			'headerStyle'=>'background:#59bb9b; color:#FFF; font-size:14px; font-weight:bold;text-align:center; border:0.5pt solid #ffffff;',
			'row1Style'=>'background:#bce4d7;',
			'row2Style'=>'background:#ffffff;',
			'boderColor'=>'#CCCCCC;'
		),$cfg);
		if(empty($field)){
			$field = array_keys($data[key($data)]);
			$field = array_combine($field,$field);
		}
		$style = '<style>'.
						'.xxx{border:0.5pt solid '.rtrim(rtrim($cfg['boderColor']),'; ').';}'.
						//'.xxx td{padding:5px 10px 5px 10px; text-align:left;}'.
						'.xxx .xrow1 td{'.$cfg['row1Style'].'}'.
						'.xxx .xrow2 td{'.$cfg['row2Style'].'}'.
						'.xxx .xtitle td{'.$cfg['headerStyle'].'}'.
						$cfg['style'].
					'</style>';
		echo '<html xmlns="http://www.w3.org/1999/xhtml">'.
				'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.
				'<title>'.$fileName.'</title>'.$style.
				'</head><body><table cellpadding="0" cellspacing="1" class="xxx">';
		if($top){
			echo '<tr class="xtitle">';
			foreach ($field as $key=> $v){
				$cls = '';
				if(is_array($v)){
					$field[$key] = $v = array_merge(array('header'=>'','headerCls'=>'','contentCls'=>''),$v);
					$cls = ( empty($v['headerCls'])? '': ' class="'.$v['headerCls'].'"');
				}
				echo '<td'.$cls.'>'.htmlspecialchars($v).'</td>';
			}
			echo '</tr>';
		}
		$i = 0;
		foreach ($data as $v){
			echo (($i%2)===0? '<tr class="xrow1">': '<tr class="xrow2">');
			foreach ($field as $k=>$FieldV){
				$cls = (is_array($FieldV) &&  !empty($FieldV['contentCls'])? ' class="'.$FieldV['contentCls'].'"' : '');
				if(isset($v[$k]))echo '<td'.$cls.'>'.htmlspecialchars($v[$k]).'</td>';
				else echo '<td'.$cls.'>&nbsp;</td>';
			}
			echo '</tr>';
		}
		echo '</table></body></html>';
		exit;
	}
}